# resample 数据重采样是将时间序列从一个频率转换至另一个频率的过程，
# 它主要有两种实现方式，分别是降采样和升采样，降采样指将高频率的数据转换为低频率，升采样则与其恰好相反
# 降采样	将高频率(间隔短)数据转换为低频率(间隔长)。
# 升采样	将低频率数据转换为高频率。

import pandas as pd
import numpy as np

rng = pd.date_range('1/1/2021', periods=100, freq='D')
print(rng)
# DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03', '2021-01-04',
#                '2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08',
#                '2021-01-09', '2021-01-10', '2021-01-11', '2021-01-12',
#                '2021-01-13', '2021-01-14', '2021-01-15', '2021-01-16',
#                '2021-01-17', '2021-01-18', '2021-01-19', '2021-01-20',
#                '2021-01-21', '2021-01-22', '2021-01-23', '2021-01-24',
#                '2021-01-25', '2021-01-26', '2021-01-27', '2021-01-28',
#                '2021-01-29', '2021-01-30', '2021-01-31', '2021-02-01',
#                '2021-02-02', '2021-02-03', '2021-02-04', '2021-02-05',
#                '2021-02-06', '2021-02-07', '2021-02-08', '2021-02-09',
#                '2021-02-10', '2021-02-11', '2021-02-12', '2021-02-13',
#                '2021-02-14', '2021-02-15', '2021-02-16', '2021-02-17',
#                '2021-02-18', '2021-02-19', '2021-02-20', '2021-02-21',
#                '2021-02-22', '2021-02-23', '2021-02-24', '2021-02-25',
#                '2021-02-26', '2021-02-27', '2021-02-28', '2021-03-01',
#                '2021-03-02', '2021-03-03', '2021-03-04', '2021-03-05',
#                '2021-03-06', '2021-03-07', '2021-03-08', '2021-03-09',
#                '2021-03-10', '2021-03-11', '2021-03-12', '2021-03-13',
#                '2021-03-14', '2021-03-15', '2021-03-16', '2021-03-17',
#                '2021-03-18', '2021-03-19', '2021-03-20', '2021-03-21',
#                '2021-03-22', '2021-03-23', '2021-03-24', '2021-03-25',
#                '2021-03-26', '2021-03-27', '2021-03-28', '2021-03-29',
#                '2021-03-30', '2021-03-31', '2021-04-01', '2021-04-02',
#                '2021-04-03', '2021-04-04', '2021-04-05', '2021-04-06',
#                '2021-04-07', '2021-04-08', '2021-04-09', '2021-04-10'],
#               dtype='datetime64[ns]', freq='D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
print(ts)
# 2021-01-01    0.110714
# 2021-01-02    1.020407
# 2021-01-03   -1.361763
# 2021-01-04    0.650342
# 2021-01-05    1.015801
#                 ...
# 2021-04-06    1.955393
# 2021-04-07   -1.007048
# 2021-04-08   -1.146712
# 2021-04-09   -0.887190
# 2021-04-10    0.971544
# Freq: D, Length: 100, dtype: float64

# 降采样后并聚合
print(ts.resample('M').mean())
# 2021-01-31   -0.025387
# 2021-02-28    0.341078
# 2021-03-31    0.286354
# 2021-04-30    0.301307
# Freq: M, dtype: float64

# 如果您只想看到月份，那么您可以设置kind=period如下所示：
print(ts.resample('M', kind='period').mean())
# 2021-01   -0.025387
# 2021-02    0.341078
# 2021-03    0.286354
# 2021-04    0.301307
# Freq: M, dtype: float64

# 升采样是将低频率（时间间隔）转换为高频率
# 生成一份时间序列数据
rng = pd.date_range('1/1/2021', periods=20, freq='3D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
print(ts.head())
# 2021-01-01   -0.100792
# 2021-01-04   -0.768405
# 2021-01-07    0.977892
# 2021-01-10    0.094077
# 2021-01-13   -0.675896
# Freq: 3D, dtype: float64

# 使用asfreq()在原数据基础上实现频率转换
print(ts.resample('D').asfreq().head())
# 2021-01-01   -0.100792
# 2021-01-02         NaN
# 2021-01-03         NaN
# 2021-01-04   -0.768405
# 2021-01-05         NaN
# Freq: D, dtype: float64


# 频率转换
index = pd.date_range('1/1/2021', periods=6, freq='T')
series = pd.Series([0.0, None, 2.0, 3.0, 4.0, 5.0], index=index)
df = pd.DataFrame({'s': series})
print(df.asfreq("45s"))
# 2021-01-01 00:00:00  0.0
# 2021-01-01 00:00:45  NaN
# 2021-01-01 00:01:30  NaN
# 2021-01-01 00:02:15  NaN
# 2021-01-01 00:03:00  3.0
# 2021-01-01 00:03:45  NaN
# 2021-01-01 00:04:30  NaN

# 插值处理
# pad/ffill	用前一个非缺失值去填充缺失值。
# backfill/bfill	用后一个非缺失值去填充缺失值。
# interpolater('linear')	线性插值方法。
# fillna(value)	指定一个值去替换缺失值。

# 创建时间序列数据
rng = pd.date_range('1/1/2021', periods=20, freq='3D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
print(ts.resample('D').asfreq().head())
# 2021-01-01   -0.207867
# 2021-01-02         NaN
# 2021-01-03         NaN
# 2021-01-04    1.560310
# 2021-01-05         NaN
# Freq: D, dtype: float64

# 使用ffill处理缺失值
print(ts.resample('D').asfreq().ffill().head())
# 2021-01-01   -0.207867
# 2021-01-02   -0.207867
# 2021-01-03   -0.207867
# 2021-01-04    1.560310
# 2021-01-05    1.560310
# Freq: D, dtype: float64